package com.dkd.flow.rules.empty;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.flow.emums.RuleType;
import com.dkd.flow.rules.BaseRules;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
//Easy Rules 是一个简单的 Java 规则引擎，用于实现基于规则的系统
@Rule(name = "EmptyRule", description = "空条件", priority = 1)
public class EmptyRule {
    @Autowired
    BaseRules baseRules;
    JSONObject properties = null;
    JSONObject node = null;
    void clear() {
        node = null;
        properties = null;
    }
    @Condition
    public boolean condition(@Fact("node") JSONObject node) {
        this.clear();
        this.node = node;
        return getMatchConditions();
    }
    @Action
    public void action() {
        setResult(true);
    }
    boolean getMatchConditions() {
        properties = node.getJSONObject("properties");
        JSONArray conditions = properties.getJSONArray("conditions");
        return conditions == null || conditions.size() < 1;
    }
    void setResult(Boolean bool) {
        JSONObject emptyRule = new JSONObject();
        emptyRule.put("result", bool);
        emptyRule.put("nodeId", node.getString("nodeId"));
        emptyRule.put("ruleType", RuleType.EMPTY_RULE.value);
        emptyRule.put("ruleTypeName", RuleType.EMPTY_RULE.name);
        emptyRule.put("priority", properties.getIntValue("priority"));
        baseRules.ruleResult.add(emptyRule);
    }
}
